package DoExercise.HSP_ZCY.A008_树.A001_普通二叉树.二叉树递归套路_找可能性;

import java.util.ArrayList;

public class Code03_是否是二叉搜索树
{
    
    public static class Node
    {
        public int value;
        public Node left;
        public Node right;
        
        public Node(int data)
        {
            this.value = data;
        }
    }
    
    public static boolean isBST1(Node head)
    {
        if (head == null)
        {
            return true;
        }
        ArrayList<Node> arr = new ArrayList<>();
        in(head, arr);
        for (int i = 1; i < arr.size(); i++)
        {
            if (arr.get(i).value <= arr.get(i - 1).value)
            {
                return false;
            }
        }
        return true;
    }
    
    public static void in(Node head, ArrayList<Node> arr)
    {
        if (head == null)
        {
            return;
        }
        in(head.left, arr);
        arr.add(head);
        in(head.right, arr);
    }
    
    public static boolean isBST2(Node head)
    {
        if (head == null)
        {
            return true;
        }
        return process(head).isBST;
    }
    
    public static class Info
    {
        boolean isBST;
        public int min;
        public int max;
        
        public Info(boolean is, int mi, int ma)
        {
            isBST = is;
            min = mi;
            max = ma;
        }
    }
    
    public static Info process(Node head)
    {
        if (head == null) return null;
        Info leftInfo = process(head.left);
        Info rightInfo = process(head.right);
        int min = head.value;
        int max = head.value;
        if (leftInfo != null)
        {
            min = Math.min(min, leftInfo.min);
            max = Math.max(max, leftInfo.max);
        }
        if (rightInfo != null)
        {
            min = Math.min(min, rightInfo.min);
            max = Math.max(max, rightInfo.max);
        }
        boolean isBST = false;
        //左树是空 || 左树是 && 左树的最大值 < 头节点的值 ......
        if ((leftInfo == null ? true : (leftInfo.isBST && leftInfo.max < head.value)) && (rightInfo == null ? true : (rightInfo.isBST && rightInfo.min > head.value)))
        {
            isBST = true;
        }
        return new Info(isBST, min, max);
    }
    
    // for test
    public static Node generateRandomBST(int maxLevel, int maxValue)
    {
        return generate(1, maxLevel, maxValue);
    }
    
    // for test
    public static Node generate(int level, int maxLevel, int maxValue)
    {
        if (level > maxLevel || Math.random() < 0.5)
        {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }
    
    public static void main(String[] args)
    {
        int maxLevel = 4;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++)
        {
            Node head = generateRandomBST(maxLevel, maxValue);
            if (isBST1(head) != isBST2(head))
            {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }
    
}
